package com.hkbigdata.datagovernance.governance.assess.calc;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hkbigdata.datagovernance.ds.bean.TDsTaskInstance;
import com.hkbigdata.datagovernance.ds.service.TDsTaskInstanceService;
import com.hkbigdata.datagovernance.governance.assess.Assessor;
import com.hkbigdata.datagovernance.governance.bean.AssessParam;
import com.hkbigdata.datagovernance.governance.bean.GovernanceAssessDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Component("TASK_FAILED")
public class TaskFailedAssessor extends Assessor {
    @Autowired
    TDsTaskInstanceService tDsTaskInstanceService;

    @Override
    public void assess(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws  Exception {

        QueryWrapper<TDsTaskInstance> queryWrapper = new QueryWrapper<TDsTaskInstance>()
                .eq("date_format(start_time,'%Y-%m-%d')", assessParam.getAssessDate())  //当日
                .eq("name", assessParam.getTableMetaInfo().getSchemaName() + "." + assessParam.getTableMetaInfo().getTableName()) //对应库名.表名的任务
                .eq("state",6);  //状态为失败
        List<TDsTaskInstance> failTaskInstanceList = tDsTaskInstanceService.list(queryWrapper);

        if(failTaskInstanceList.size()>0){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("存在失败任务");
        }
    }
}

